﻿using System;
using System.Drawing;

namespace NExplorer
{
	/// <summary>
	/// Command that's enabled when one or more items are selected.
	/// </summary>
	public abstract class AnyItemsCommandBase : CommandBase
	{
		public AnyItemsCommandBase()
		{
			ServiceManager.Explorer.SelectedItemsChanged += WhenSelectedItemsChanged;
		}

		protected AnyItemsCommandBase(string text)
			: base(text, null)
		{}

		protected AnyItemsCommandBase(string text, Image image)
			: base(text, image)
		{}

		private void WhenSelectedItemsChanged(object sender, EventArgs e)
		{
			SetEnabled();
		}

		private void SetEnabled()
		{
			Enabled = ServiceManager.Explorer.SelectedItems.Count > 0;
		}

		public override void Dispose()
		{
			base.Dispose();

			ServiceManager.Explorer.SelectedItemsChanged -= WhenSelectedItemsChanged;
		}
	}
}
